package com.senseilearning.sensei;

import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListActivity;

public class WordsViewActivity extends SherlockListActivity implements
		OnInitListener {
	private Section section;
	private TtsHandler tts;
	ArrayList<RowModel> list;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chapters_list);
		Intent intent = getIntent();
		int chapterId = intent.getIntExtra("chapter", 0);
		int sectionId = intent.getIntExtra("section", 0);
		section = Chapters.getSection(chapterId,sectionId);
		ActionBar ab = getSupportActionBar();
		ab.setTitle(section.getName());
		tts = new TtsHandler(this, this,"es","ESP");
		list = new ArrayList<RowModel>();
		for (int i = 0; i < section.size(); i++) {
			list.add(new RowModel(section.get(i).getWord1(), section.get(i)
					.getWord2()));
		}
		setListAdapter(new WordlistAdapter(list));
	}

	/*
	 * //onStart and onStop are now only used for calling the analytics methods
	 * 
	 * @Override public void onStart(){ super.onStart(); //New code for this
	 * method must be added before the easytracker line!
	 * EasyTracker.getInstance().activityStart(this); Tracker tracker =
	 * EasyTracker.getTracker(); tracker.setCustomDimension(2,
	 * section.getName()); //misschien ook een chapter meegeven? dimension id
	 * voor een chapter is 1, voor een section 2. tracker.sendView(); }
	 */

	/*
	 * @Override public void onStop() { super.onStop(); //New code for this
	 * method must be added before the easytracker line!
	 * EasyTracker.getInstance().activityStop(this); }
	 */
	
	
	@Override
	public void onInit(int status) {
		tts.init(status);
	}
	
	
	@Override
	public void onDestroy() {
		if(tts != null){
			tts.shutdown();
		}
		super.onDestroy();
	}



	@Override
	public void onListItemClick(ListView parent, View v, int position, long id) {
		tts.speakOut(list.get(position).word2);
	}

	private RowModel getModel(int position) {
		return (((WordlistAdapter) getListAdapter()).getItem(position));
	}

	class WordlistAdapter extends ArrayAdapter<RowModel> {
		WordlistAdapter(ArrayList<RowModel> list) {
			super(WordsViewActivity.this, R.layout.row_word, list);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			ViewWrapper wrapper;
			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.row_word, parent, false);
				wrapper = new ViewWrapper(row);
				row.setTag(wrapper);

			} else {
				wrapper = (ViewWrapper) row.getTag();
			}
			RowModel model = getModel(position);
			wrapper.getLabel().setText(model.getWord1());
			wrapper.getLabel2().setText(model.getWord2());
			return (row);
		}
	}

	class ViewWrapper {
		View base;
		RatingBar rate = null;
		TextView label = null;
		TextView label2 = null;
		ViewWrapper(View base) {
			this.base = base;
		}
		TextView getLabel() {
			if (label == null) {
				label = (TextView) base.findViewById(R.id.word1);
			}
			return (label);
		}
		TextView getLabel2() {
			if (label2 == null) {
				label2 = (TextView) base.findViewById(R.id.word2);
			}
			return (label2);
		}
	}

	class RowModel {
		String word1;
		String word2;

		RowModel(String word1, String word2) {
			this.word1 = word2;
			this.word2 = word1;
		}

		public String getWord1() {
			return word1;
		}

		public String getWord2() {
			return word2;
		}
	}

}